1. MongoDB Introduction
Definition
MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. It provides high performance, high availability, and easy scalability. Documents in MongoDB can have different fields, and the structure can be changed over time.
Algorithm 1: Creating a Database :-
use mydatabase
db.createCollection("mycollection")
Example 1: Creating and Inserting Documents
// Insert a document
db.users.insertOne({
name: "John Doe",
age: 30,
email: "john@example.com"
})
Algorithm 2: Querying Documents :-
db.collection
Example 2: Querying Documents
// Find documents
db.users.find({
age: { $gt: 25 },
email: { $exists: true }
}).sort({ name: 1 })
Algorithm 3: Updating Documents :-
Example 3: Updating Documents
// Update documents
db.users.updateMany(
{ age: { $lt: 30 } },
{ $set: { status: "young" } }
)